﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WirtualneNauczanie.Web
{
    public partial class CourseText : System.Web.UI.Page
    {
        String yourcourseid, section; 
        protected void Page_Load(object sender, EventArgs e)
        {
            yourcourseid = Request.QueryString["yourcourseid"];
            section = Request.QueryString["section"];
            
            
            SqlDataSource1.SelectCommand = "SELECT SectionContents.SectionContentTitle, SectionContents.SectionContent FROM CourseSections INNER JOIN SectionContents ON CourseSections.CourseSectionID = SectionContents.CourseSectionID WHERE (CourseSections.CourseSectionID = " + section +  ")";
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            String sectionTitleString = (String)dv.Table.Rows[0][0];
            String sectionContentString = (String)dv.Table.Rows[0][1];

            TitleLabel.Text = sectionTitleString;
            SectionContentLabel.Text = sectionContentString;

        }
        int nextSection;
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlDataSource1.SelectCommand = "SELECT CourseSectionID FROM CourseSections WHERE (CourseSectionID > " + section + ") AND (CourseID = " + yourcourseid + ") ORDER BY CourseSectionID";
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            
            //int nextSection = (int)dv.Table.Rows[0][0];
            
            if (dv.Table.Rows.Count == 0)
            {
                Button1.Visible = false;
            }
            else
            {
                Button2.Visible = true;
                nextSection = (int)dv.Table.Rows[0][0];
                Response.Redirect("CourseText.aspx?yourcourseid=" + yourcourseid + "&section=" + nextSection);
            }
            

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SqlDataSource1.SelectCommand = "SELECT CourseSectionID FROM CourseSections WHERE (CourseSectionID < " + section + ") AND (CourseID = " + yourcourseid + ") ORDER BY CourseSectionID DESC";
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

            //int nextSection = (int)dv.Table.Rows[0][0];

            if (dv.Table.Rows.Count == 0)
            {
                Button2.Visible = false;
            }
            else
            {
                Button1.Visible = true;
                nextSection = (int)dv.Table.Rows[0][0];
                Response.Redirect("CourseText.aspx?yourcourseid=" + yourcourseid + "&section=" + nextSection);
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Course.aspx?yourcourseid=" + yourcourseid);
        }
    }
}
